package dianmobile.eistarandroid.ui.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.MyNewsEntity;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.ui.activities.UserInfoActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.DataUtils;

/**
 * Created by dianmobile_yy on 15/10/25.
 */
public class MyActivityAdapter extends BaseAdapter {

    private Context context;
    private List<MyNewsEntity> listData;
    private LayoutInflater inflater;

    public MyActivityAdapter(Context context, List<MyNewsEntity> listData) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ViewHolder viewHolder;
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.listitem_my_activities, null);
            viewHolder = new ViewHolder();
            viewHolder.userHeader = (CircleImageView)convertView.findViewById(R.id.iv_senderHead);
            viewHolder.userName = (TextView)convertView.findViewById(R.id.tv_senderName);
            viewHolder.boardName = (TextView)convertView.findViewById(R.id.tv_board_name);
            viewHolder.time = (TextView)convertView.findViewById(R.id.tv_time);
            viewHolder.title = (TextView)convertView.findViewById(R.id.tv_title);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        final String userName = listData.get(position).getUserName();
        String url = DataUtils.getUserHeadUrlFromName(userName);
        RequestFactory.makeNetworkImage(viewHolder.userHeader, url, R.drawable.default_face, 0);
        viewHolder.userName.setText(userName);
        viewHolder.boardName.setText(listData.get(position).getBoardName());
        viewHolder.time.setText(listData.get(position).getTime());
        viewHolder.title.setText(listData.get(position).getTitle());

        viewHolder.userHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra(ConstantsData.EXTRA_USER_NAME, userName);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder {
        CircleImageView userHeader;
        TextView userName;
        TextView boardName;
        TextView time;
        TextView title;
    }
}
